package portal.web.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import portal.excepciones.EliminarObjetoException;
import portal.facade.PortalComprasFacadeStateful;

@SuppressWarnings("serial")
public class EliminarDeCarritoServlet extends HttpServlet {

	private static Logger logger = Logger
			.getLogger(EliminarDeCarritoServlet.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}

	private void processRequest(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>EliminarDeCarritoServlet<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
		HttpSession session = req.getSession(true);
		PortalComprasFacadeStateful facadeStateful = (PortalComprasFacadeStateful) session.getAttribute("facadeStateful");
		
		Long idProducto = Long.parseLong((String) req.getParameter("id"));

		try {
			facadeStateful.eliminarItem(idProducto);
			req.setAttribute("listaCarrito", facadeStateful.getListaItems());
			req.getRequestDispatcher("/jsp/carritoCompras.jsp").forward(req,
					resp);
		} catch (EliminarObjetoException e) {
			req.getRequestDispatcher("/jsp/error.jsp").forward(req, resp);
			logger.warn(e.getMessage());
			e.printStackTrace();
		}

	}
}
